package cn.tedu.upload.controller;

import cn.tedu.upload.pojo.entity.Upload;
import cn.tedu.upload.pojo.vo.UploadVO;
import cn.tedu.upload.service.UploadService;
import cn.tedu.upload.utils.JsonResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/v1/upload")
public class UploadController {
    @Autowired
    private UploadService uploadService;

    @ApiOperation("单文件上传")
    @PostMapping("upload")
    public JsonResult upload(@RequestParam("file") MultipartFile file) {
        Upload upload = uploadService.upload(file);
        UploadVO uploadVO = new UploadVO();
        BeanUtils.copyProperties(upload, uploadVO);
        return JsonResult.ok(uploadVO);
    }
}
